import store from '@/store';
import Vue from 'vue';
import VueRouter from 'vue-router';

const Home = () => import('@/views/home/Home');
const Search = () => import('@/views/home/Search');
const Vod = () => import('@/views/vod/Vod');
const Video = () => import('@/views/vod/Video');
const Detail = () => import('@/views/Detail');
const Study = () => import('@/views/Study');
const Member = () => import('@/views/member/Member');
const Setting = () => import('@/views/member/Setting');
const Login = () => import('@/views/member/Login');
const Messages = () => import('@/views/member/Messages');
const Profile = () => import('@/views/member/Profile');
const Role = () => import('@/views/member/Role');
const MyOrder = () => import('@/views/member/MyOrder');
const Order = () => import('@/views/order/Order');
const PayOrder = () => import('@/views/order/PayOrder');
const Promocode = () => import('@/views/member/Promocode');
const Register = () => import('@/views/member/Register');

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    meta: {
      title: '首页',
    },
  },
  {
    path: '/search',
    name: 'search',
    component: Search,
    meta: {
      title: '搜索',
    },
  },
  {
    path: '/vod',
    name: 'vod',
    component: Vod,
    meta: {
      title: '录播课程',
    },
  },
  {
    path: '/vod/video',
    name: 'video',
    component: Video,
    meta: {
      title: '视频播放',
      isAuthRequired: true,
    },
  },
  {
    path: '/vod/detail',
    name: 'detail',
    component: Detail,
    meta: {
      title: '课程详情',
    },
  },
  {
    path: '/study',
    name: 'study',
    component: Study,
    meta: {
      title: '我的课程',
    },
  },
  {
    path: '/member',
    name: 'member',
    component: Member,
    meta: {
      title: '会员中心',
    },
  },
  {
    path: '/member/setting',
    name: 'setting',
    component: Setting,
    meta: {
      title: '设置',
    },
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: '用户登录',
    },
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
    meta: {
      title: '用户注册',
    },
  },
  {
    path: '/messages',
    name: 'messages',
    component: Messages,
    meta: {
      title: '我的消息',
      isAuthRequired: true,
    },
  },
  {
    path: '/member/profile',
    name: 'profile',
    component: Profile,
    meta: {
      title: '个人资料',
      isAuthRequired: true,
    },
  },
  {
    path: '/role',
    name: 'role',
    component: Role,
    meta: {
      title: 'VIP会员',
      isAuthRequired: true,
    },
  },
  {
    path: '/member/order',
    name: 'myorder',
    component: MyOrder,
    meta: {
      title: '我的订单',
      isAuthRequired: true,
    },
  },
  {
    path: '/order',
    name: 'order',
    component: Order,
    meta: {
      title: '收银台',
      isAuthRequired: true,
    },
  },
  {
    path: '/order/pay',
    name: 'payorder',
    component: PayOrder,
    meta: {
      title: '收银台',
      isAuthRequired: true,
    },
  },
  {
    path: '/member/promocode',
    name: 'promocode',
    component: Promocode,
    meta: {
      title: '我的邀请码',
      isAuthRequired: true,
    },
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  store.dispatch('updateToken');
  if (to.meta.isAuthRequired && store.state.user.isLogin == false) {
    return next({ path: '/register', query: { url: location.href } });
  }
  next();
  document.title = to.meta.title;
});

export default router;
